package com.example.zsfinacialmanage.other;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.activity.SysSettingActivity;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.utils.Md5Util;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.utils.UserExistQuery;
import com.example.zsfinacialmanage.view.RecoverActivity;
import com.example.zsfinacialmanage.view.RegisterActivity;

public class ReviseInfoActivity extends AppCompatActivity {

    EditText et_name,et_newpwd,et_email,et_phone,et_oldpwd;
    Button btn_ok,btn_back,btn_forgot;
    String db_name,db_pwd,db_email,db_phone;
    MyDBHelper myDBHelper;
    SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revise_info);
        //绑定控件
        initView();
        //数据
        //确定修改按钮功能实现
        btnOk();
        //返回按钮功能实现
        btnBack();
        //忘记密码
        btnForgot();
    }

    private void btnForgot() {
        btn_forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ReviseInfoActivity.this, RecoverActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initView() {
        et_name = findViewById(R.id.et_name_rev);
        et_newpwd = findViewById(R.id.et_newpwd_rev);
        et_email = findViewById(R.id.et_email_rev);
        et_phone = findViewById(R.id.et_phone_rev);
        et_oldpwd = findViewById(R.id.et_oldpwd_rev);
        btn_ok = findViewById(R.id.bt_ok_rev);
        btn_back = findViewById(R.id.bt_back_rev);
        btn_forgot = findViewById(R.id.bt_forgot_re);
        myDBHelper = new MyDBHelper(ReviseInfoActivity.this);
        db = myDBHelper.getWritableDatabase();
    }

    private void btnOk() {
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                //1.获取数据库数据
                String userName = getSharedPreferences("userinfo",0).getString("userName","");
                @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from tb_userinfo where name = ?",new String[]{userName});
                if (cursor.moveToNext()){
                    db_name = cursor.getString(cursor.getColumnIndex("name"));
                    db_phone = cursor.getString(cursor.getColumnIndex("phone"));
                    db_email = cursor.getString(cursor.getColumnIndex("email"));
                    db_pwd = cursor.getString(cursor.getColumnIndex("pwd"));
                }
                //2.获取用户输入数据
                String inputName = et_name.getText().toString();
                String inputNewPwd = Md5Util.get(et_newpwd.getText().toString());
                String inputEmail = et_email.getText().toString();
                String inputPhone = et_phone.getText().toString();
                String inputOldPwd = Md5Util.get(et_oldpwd.getText().toString());
                /**
                 * 3.判断用户输入的是否为空，如果是空数据则用原数据代替
                 */
                //判断用户名是否已存在
                if(UserExistQuery.isUserExists(inputName,db)){
                    //存在
                    //弹窗提示"用户存在"
                    Toast.makeText(ReviseInfoActivity.this,inputName + MessageConstant.ALREADY_EXISTS,Toast.LENGTH_SHORT).show();
                    et_name.setText("");
                }else {
                    //不存在
                    //判断密码是否正确
                    if (inputOldPwd.equals(db_pwd)){
                        //为空的项目用原值
                        //用户名
                        if (inputName.equals("")){
                            inputName = db_name;
                        }
                        //新密码
                        if (inputNewPwd.equals("")){
                            inputNewPwd = db_pwd;
                        }
                        //邮箱
                        if (inputEmail.equals("")){
                            inputEmail = db_email;
                        }
                        //电话
                        if (inputPhone.equals("")){
                            inputPhone = db_phone;
                        }
                        //赋值
                        ContentValues values = new ContentValues();
                        values.put("name",inputName);
                        values.put("pwd",inputNewPwd);
                        values.put("email",inputEmail);
                        values.put("phone",inputPhone);
                        db.update("tb_userinfo",values,"name = ?",new String[]{userName});
                        Toast.makeText(ReviseInfoActivity.this, MessageConstant.REVISE_SUCCESS, Toast.LENGTH_SHORT).show();
                        //刷新页面
                        Intent intent = new Intent(ReviseInfoActivity.this, ReviseInfoActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        //密码不正确
                        Toast.makeText(ReviseInfoActivity.this, MessageConstant.PASSWORD_ERROR,Toast.LENGTH_SHORT).show();
                        et_oldpwd.setText("");
                    }
                }
            }
        });
    }

    private void btnBack() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ReviseInfoActivity.this,SysSettingActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}

